//
//  UIViewController+extension.m
//  BASE_LIB
//
//  Created by shiji on 2018/10/24.
//  Copyright © 2018年 shiji. All rights reserved.
//

#import "UIViewController+extension.h"

@implementation UIViewController (extension)
+ (UIViewController *)currentViewController {
    id<UIApplicationDelegate>delegate = [UIApplication sharedApplication].delegate;
    UIWindow *window = [delegate respondsToSelector:@selector(window)] ? [delegate window] : nil;
    if (window.rootViewController) {
        // 如果当前是VC的情况
        return [self topViewControllerWithRootViewController:window.rootViewController];
    }else {
        // 如果当前是自定义的window需要查找vc
        for (UIWindow *window in [UIApplication sharedApplication].windows) {
            if (window.rootViewController) {
                return [self topViewControllerWithRootViewController:window.rootViewController];
                break;
            }
        }
    }
    return nil;
}

+ (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
    // Handling UITabBarController
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    }
    // Handling UINavigationController
    else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    }
    // Handling Modal views
    else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    }
    // Handling UIViewController's added as subviews to some other views.
    else {
        for (UIView *view in [rootViewController.view subviews])
        {
            id subViewController = [view nextResponder];    // Key property which most of us are unaware of / rarely use.
            if ( subViewController && [subViewController isKindOfClass:[UIViewController class]])
            {
                return [self topViewControllerWithRootViewController:subViewController];
            }
        }
        return rootViewController;
    }
}

- (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].delegate.window rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
}

@end
